package com.google.code.ptrends.catparsers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.code.ptrends.catparsers.utils.RegexHelper;
import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.Property;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.entities.ETLItem;
import com.google.code.ptrends.entities.ParametersStrings;
import com.google.code.ptrends.excel.ExcelReader;
import com.google.code.ptrends.excel.entities.Worksheet;

public class DriveCategoryParser extends AbstractCategoryParser {
	private final List<Matcher> matcher;

	public DriveCategoryParser(final ExcelReader reader,
			final Worksheet worksheet, final Session session) {
		super(reader, worksheet, session);
		matcher = new ArrayList<Matcher>();
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive1")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive2")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive3")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive4")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive5")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("drive6")).matcher(""));
	}

	@Override
	protected ETLItem createItem(final String description, final String priceUSD) {
		ETLItem item = null;

		if (StringUtils.isNotBlank(description)
				&& StringUtils.isNotBlank(priceUSD)) {
			for (Matcher m : matcher) {
				m.reset(description);
				if (m.find()) {
					final java.util.List<Property> property = new ArrayList<Property>();

					if (StringUtils.isNotBlank(m.group(3))) {
						item = buildItem(Category.DRIVE, m.group(2),
								m.group(3), new Float(priceUSD), new Date());
					} else {
						item = buildItem(Category.DRIVE, m.group(2), "OEM",
								new Float(priceUSD), new Date());
					}

					if (StringUtils.isNotBlank(m.group(1))) {
						property.add(buildProperty(ParametersStrings.TYPE,
								m.group(1))); // Type
					}
					if (StringUtils.isNotBlank(m.group(4))) {
						property.add(buildProperty(ParametersStrings.INTERFACE,
								m.group(4))); // Interface
					}
					if (StringUtils.isNotBlank(m.group(5))) {
						property.add(buildProperty(ParametersStrings.BD_DVDD,
								m.group(5))); // BD/DVD-Dual
						// write/read
						// speed
					}
					if (StringUtils.isNotBlank(m.group(6))) {
						property.add(buildProperty(ParametersStrings.DVDD_DVD,
								m.group(6))); // DVD-Dual/DVD
						// write/read
						// speed
					}
					if (StringUtils.isNotBlank(m.group(7))) {
						property.add(buildProperty(ParametersStrings.DVD_DVDR,
								m.group(7))); // DVD/DVD-RAM
						// write/read
						// speed
					}

					item.setParameters(property);
					break;
				}
			}
		}
		return item;
	}
}
